package Java8NewLearn.StreamApi;

import Java8NewLearn.MethodsQuote.Employee;
import Java8NewLearn.MethodsQuote.EmployeeData;
import org.junit.jupiter.api.Test;


import java.sql.SQLOutput;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Lenl
 * @version v1.0
 * @create 2021-06-08 12:56
 * @description Stream的中止操作
 *
 *
 */
public class StreamAPITest2 {
    //1-匹配与查找
    @Test
    public void test1(){
        List<Employee> employees= EmployeeData.getEmployees();
        //allMatch(Predicate p)-检查是否匹配所有元素
        //练习：是否所有的员工年龄都大于18
        boolean allMatch= employees.stream().allMatch(t->t.getAge()>18);
        System.out.println(allMatch);
        //anyMatch(Predicate p)-检查是否至少匹配一个元素
        boolean anyMatch=employees.stream().anyMatch(t->t.getSalary()>5000);
        System.out.println(anyMatch);
        //noneMatch(Predicate p)-检查是否没有匹配的元素。练习：是否存在员工姓“雷”
        boolean noneMatch=employees.stream().noneMatch(t->t.getName().startsWith("雷"));
        System.out.println(noneMatch); //返回none则代表有员工姓“雷”
        //findFirst-返回第一个元素
        Optional<Employee> first = employees.stream().findFirst();
        System.out.println(first);
        //findAny-返回当前元素的任意元素
        Optional<Employee> any = employees.parallelStream().findAny();
        System.out.println(any);
    }

    @Test
    public void test2(){
        List<Employee> employees= EmployeeData.getEmployees();
        //count-返回流中元素的个数
        System.out.println(employees.stream().filter(t->t.getSalary()>5000).count());
        //max(Comparator c)-返回流中最大值
        //练习：返回最高工资
        Optional<Double> max = employees.stream().map(t -> t.getSalary()).max(Double::compare);
        System.out.println(max);
        //min(Comparator c)-返回流中最小值
        //练习：返回最低工资的员工
        Optional<Employee> min = employees.stream().min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
        System.out.println(min);
        //forEach(Consumer c)-内部迭代
        employees.stream().forEach(System.out::println);

        //集合迭代
        employees.forEach(System.out::println);
    }

    //2-归约
    @Test
    public void test3(){
        //reduce(T identity,BinaryOperator) --可以将流中元素反复结合起来，得到一个值。返回T
        //练习：计算1-10的自然数的和
        List<Integer> list= Arrays.asList(1,2,3,4,5,6,7,8,9,10);
        Integer sum = list.stream().reduce(0, Integer::sum);
        System.out.println(sum);

        //reduce(BinaryOperator) --可以将流中元素反复结合起来，得到一个值。返回Optional<T>
        //练习2：计算公司所有员工工资的总和
        List<Employee> employees = EmployeeData.getEmployees();
        Stream<Double> salaryStream = employees.stream().map(t -> t.getSalary());
        Optional<Double> sumMoney = salaryStream.reduce(Double::sum);
        System.out.println(sumMoney.get());//Optional.get()表示获取值
    }

    //3-收集
    @Test
    public void test4(){
        //collect(Collector c)--将流转换为其他形式。接收一个Collector接口的实现，用于给Stream中元素做汇总的方法
        //练习1:查找工资大于6000的员工，结果返回为一个List或Set
        List<Employee> employees = EmployeeData.getEmployees();
        List<Employee> collect = employees.stream().filter(t -> t.getSalary() > 6000).collect(Collectors.toList());
        collect.forEach(System.out::println);

        Set<Employee> collect1 = employees.stream().filter(t -> t.getSalary() > 6000).collect(Collectors.toSet());
        collect1.forEach(System.out::println);


    }

}
